/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package loginRegister;

import java.beans.Statement;
import java.io.IOException;
import java.io.PrintWriter;

import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.spi.DirStateFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

/**
 *
 * @author DELL
 */
public class LoginServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet LoginServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet LoginServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    public void wrong1() {
        String msg = "用户名不能为空！";
        String title = "信息提示";
        int type = JOptionPane.YES_NO_CANCEL_OPTION;
        JOptionPane.showMessageDialog(null, msg, title, type);

    }

    public void wrong2() {
        String msg = "密码不能为空！";
        String title = "信息提示";
        int type = JOptionPane.YES_NO_CANCEL_OPTION;
        JOptionPane.showMessageDialog(null, msg, title, type);

    }

    public void wrong3() {
        String msg = "您还未注册，登录失败！请先注册！";
        String title = "信息提示";
        int type = JOptionPane.YES_NO_CANCEL_OPTION;
        JOptionPane.showMessageDialog(null, msg, title, type);

    }

    public void wrong4() {
        String msg = "用户密码不正确，请重新输入！";
        String title = "信息提示";
        int type = JOptionPane.YES_NO_CANCEL_OPTION;
        JOptionPane.showMessageDialog(null, msg, title, type);

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String userName = new String(request.getParameter("userName").getBytes("ISO-8859-1"), "utf-8");
        log(userName);
        
        String password = new String(request.getParameter("password").getBytes("ISO-8859-1"), "utf-8");
        log(password);
        if (userName.equals("")) {
            wrong1();
            response.sendRedirect("http://localhost:8081/PersonInforMSys/login.jsp");
        } else if (password.equals("")) {
            wrong2();
            response.sendRedirect("http://localhost:8081/PersonInforMSys/login.jsp");
        } else {
            try {
                Connection con = null;
                java.sql.Statement stmt = null;
                ResultSet rs = null;
                Class.forName("com.mysql.jdbc.Driver");

                String url = "jdbc:mysql://localhost:3306/person";

                con = DriverManager.getConnection(url, "skyline", "739349");
             
                stmt = con.createStatement();
                log(userName);
                String sql = "select * from user where userName='" + userName + "'";
                
                rs = stmt.executeQuery(sql);
            
                int N = 0;
                int P = 0;
                log("111111111111111111111111");
                   
                      log("111111111111111111111112");
                while (rs.next()) {
                    
                    if (userName.equals(rs.getString("userName"))) {
                        N = 101;
                        if (password.equals(rs.getString("password"))) {
                            P = 101;
                            //loginBean  用于保存当前会话的用户信息
                            LoginBean nbean = new LoginBean();
                            nbean.setUserName(userName);
                            nbean.setPassword(password);
                            HttpSession session = request.getSession();
                            ArrayList<LoginBean> login = new ArrayList<LoginBean>();
                            login.add(nbean);
                            session.setAttribute("login", login);
                            response.sendRedirect("http://localhost:8081/PersonInforMSys/main/main.jsp");
                        } else {
                        }

                    } else {
                        N++;
                    }

                }
                if (N < 101) {

                    wrong3();
                    response.sendRedirect("http://localhost:8081/PersonInforMSys/login.jsp");
                } else if (P < 101) {
                    wrong4();
                    response.sendRedirect("http://localhost:8081/PersonInforMSys/login.jsp");

                }

            } catch (SQLException ex) {
                Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
